﻿Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports CodeProject.Data.Entity
Imports InvoiceSample

<TestClass()> _
Public Class SaveProductTestsVB
	Inherits BaseInvoiceTestsVB

	<TestMethod(), RollingBackTransaction()> _
	Public Sub ChangeSupplierNoUpdateTest()

		'' Retrieve product:
		Dim prod As Product
		prod = Me.GetDetachedProductWithSupplier(10)

		Assert.AreEqual("Acme Factory Manchester", prod.Supplier.Name)

		'' Change supplier:
		prod.Supplier = Me.Suppliers(2)

		'' Change supplier properties:
		Dim supplierAddressBefore As String
		supplierAddressBefore = prod.Supplier.Address
		prod.Supplier.Address = "Moon"

		'' Save product with supplier:
		prod = Me.SaveProductWithSupplier(prod)

		'' Retrieve product from store and check supplier:
		Assert.AreEqual("Acme Factory Brighton", Me.GetDetachedProductWithSupplier(10).Supplier.Name)
		Assert.AreEqual(supplierAddressBefore, Me.GetDetachedProductWithSupplier(10).Supplier.Address)

	End Sub

	Private Function SaveProductWithSupplier(ByVal product As Product) As Product

		Using context = New SampleInvoiceContext()

			Dim attachedProduct As Product
			attachedProduct = context.AttachObjectGraph(product, Function(p) p.Supplier.WithoutUpdate())

			context.SaveChanges()

			Return attachedProduct

		End Using

	End Function

End Class
